package cn.shengongying.utils;

import com.google.gson.Gson;
import com.qiniu.common.QiniuException;
import com.qiniu.http.Response;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.UploadManager;
import com.qiniu.storage.model.DefaultPutRet;
import com.qiniu.util.Auth;

import java.io.InputStream;

/**
 * author: RXK
 * date: 2022/2/21 星期一
 * desc:
 */
public class UploadUtils {


    private static final String access_key = "sqiAB4LkqbvpnvnWEns2UYA1NqkQFO3_gkd1FM4k";
    private static final String secret_key = "FIv2gmkH0KOWLWCjezW1HbdBe17t6z10ohvBNL6V";
    private static final String bucket = "weilankeji-develop";
    private static final String domain = "http://img.shengongying.cn/";


    private static final Configuration CONFIGURATION;

    static {
        CONFIGURATION = new Configuration();
    }



    public static String upload(InputStream inputStream) throws Exception {
        UploadManager uploadManager = new UploadManager(CONFIGURATION);
        Auth auth = Auth.create(access_key, secret_key);
        try {
            Response response = uploadManager.put(inputStream, null, auth.uploadToken(bucket), null, null);
            DefaultPutRet defaultPutRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
            return defaultPutRet.hash;
        } catch (QiniuException e) {
            throw new Exception("上传文件异常",e);
        }
    }
}
